package com.cms.bs.app.service.base;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.avaje.ebean.Expr;
import com.avaje.ebean.Expression;
import com.cms.bs.util.BaseTypeUtil;

/**
 * Created by jiang on 2016-09-26.
 */
public abstract class BaseService {

    /**
     * 添加 查询条件
     *
     * @param params
     */
    public Expression addQueryParam(JSONArray params) {
        Expression expRight = null;
        String con = "and";
        for (int i = 0; i < params.size(); i++) {
            JSONObject objLeft = params.getJSONObject(i);
            String colLeft = BaseTypeUtil.safeString(objLeft.get("col"));
            String oprLeft = BaseTypeUtil.safeString(objLeft.get("opr"));
            String valLeft = BaseTypeUtil.safeString(objLeft.get("val"));
            Expression expLeft = getExpression(colLeft, valLeft, oprLeft);

            if (params.size() == 1) {
                return expLeft;
            }

            if (expRight == null) {
                JSONObject objRight = params.getJSONObject(i + 1);
                String colRight = BaseTypeUtil.safeString(objRight.get("col"));
                String oprRight = BaseTypeUtil.safeString(objRight.get("opr"));
                String valRight = BaseTypeUtil.safeString(objRight.get("val"));
                expRight = getExpression(colRight, valRight, oprRight);
                i++;
            }

            expRight = getExpressionRelation(expLeft, expRight, con);

            con = BaseTypeUtil.safeString(objLeft.getString("con"));

            // 下一级
            JSONArray children = objLeft.getJSONArray("children");
            if (children != null && children.size() > 0) {
                expRight = getExpressionRelation(addQueryParam(children), expRight, con);
            }
        }
        return expRight;
    }

    private Expression getExpression(String col, String val, String opr) {
        Expression exp = null;
        switch (opr) {
            case "eq":
                exp = Expr.eq(col, val);
                break;
            case "ne":
                exp = Expr.ne(col, val);
                break;
            case "gt":
                exp = Expr.gt(col, val);
                break;
            case "ge":
                exp = Expr.ge(col, val);
                break;
            case "lt":
                exp = Expr.lt(col, val);
                break;
            case "le":
                exp = Expr.le(col, val);
                break;
            case "lk":
                exp = Expr.like(col, val);
                break;
            case "bt":
                String[] vals = val.split(",");
                exp = Expr.between(col, vals[0], vals[1]);
                break;
        }
        return exp;
    }

    private Expression getExpressionRelation(Expression expLeft, Expression expRight, String con) {
        Expression exp = null;
        switch (con) {
            case "and":
                exp = Expr.and(expLeft, expRight);
                break;
            case "or":
                exp = Expr.or(expLeft, expRight);
                break;
        }
        return exp;
    }

}
